package com.lottery.ruok.lottrey.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.ListView;

import com.lottery.ruok.lottrey.R;
import com.lottery.ruok.lottrey.adapter.ChooseLotteryListAdapter;
import com.lottery.ruok.lottrey.retrofitModule.RetrofitBase;
import com.lottery.ruok.lottrey.retrofitModule.RxHelper;
import com.lottery.ruok.lottrey.retrofitModule.RxSubscribe;
import com.lottery.ruok.lottrey.retrofitModule.request.GetWholeCategoryRequest;
import com.lottery.ruok.lottrey.retrofitModule.response.GetWholeCategoryResponse;
import com.lottery.ruok.lottrey.retrofitModule.response.LotteryStatOneResponse;
import com.lottery.ruok.lottrey.userInfo.UserInfo;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
/*
* 选择彩种
* */

public class ChooseLotteryActivity extends BaseActivity {
    private static final String EXTRA_CODE = "code";
    public static final int REPONSE_CODE = 111;
    @BindView(R.id.listview)
    ListView listView;
    @BindView(R.id.close)
    ImageView close;

    private ArrayList<LotteryStatOneResponse.ItemListBean> item_lists = new ArrayList<>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_choose_lottery);

        ButterKnife.bind(this);

        init();
    }
    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(0, R.anim.anim_exit_from_bottom);
    }

    private void init() {
        /*item_lists = getIntent().getParcelableArrayListExtra("lists");
        if (item_lists == null) {
            item_lists = new ArrayList<>();
            loadData();
        } else {
            getList();
        }*/
        loadData();
    }

    private void loadData() {
        RetrofitBase.getRetrofit().create(GetWholeCategoryRequest.class).getResult(UserInfo.getToken(), 0, 1)
                .compose(RxHelper.<ArrayList<GetWholeCategoryResponse>>handleResult())
                .subscribe(new RxSubscribe<ArrayList<GetWholeCategoryResponse>>(ChooseLotteryActivity.this) {
                    @Override
                    protected void _onNext(ArrayList<GetWholeCategoryResponse> getWholeCategoryResponses) {

                        for (GetWholeCategoryResponse getWholeCategoryResponse : getWholeCategoryResponses) {
                            LotteryStatOneResponse.ItemListBean itemListBean = new LotteryStatOneResponse.ItemListBean();
                            if("全部".equals(getWholeCategoryResponse.getName())){
                                continue;
                            }
                            itemListBean.setNameX(getWholeCategoryResponse.getName());
                            itemListBean.setItems(new ArrayList<LotteryStatOneResponse.ItemListBean.ItemsBean>());
                            for (GetWholeCategoryResponse.ItemsBean itemsBean : getWholeCategoryResponse.getItems()) {
                                LotteryStatOneResponse.ItemListBean.ItemsBean itemsBean1 = new LotteryStatOneResponse.ItemListBean.ItemsBean();
                                itemsBean1.setNameX(itemsBean.getName());
                                itemsBean1.setCode(itemsBean.getCode());
                                itemsBean1.setItem_class(itemsBean.getItem_class());
                                itemListBean.getItems().add(itemsBean1);
                            }
                            item_lists.add(itemListBean);
                        }
                        getList();
                    }

                    @Override
                    protected boolean showDialog() {
                        return false;
                    }
                });

    }

    private void getList() {
        for (LotteryStatOneResponse.ItemListBean m : item_lists) {
            if (m.getNameX().equals("热门")) {
                item_lists.remove(m);
                item_lists.add(0, m);
                break;
            }
//            else if (m.getNameX().equals("极速彩")) {
//                LotteryStatOneResponse.ItemListBean.ItemsBean itemBean = new LotteryStatOneResponse.ItemListBean.ItemsBean();
//                itemBean.setNameX("极速六合彩");
//                itemBean.setCode("jslhc");
//                m.getItems().add(itemBean);
//            } else if (m.getNameX().equals("低频彩")) {
//                LotteryStatOneResponse.ItemListBean.ItemsBean itemBean = new LotteryStatOneResponse.ItemListBean.ItemsBean();
//                itemBean.setNameX("香港六合彩");
//                itemBean.setCode("xglhc");
//                m.getItems().add(itemBean);
//            }
        }
        ChooseLotteryListAdapter listViewAdapter = new ChooseLotteryListAdapter(this, item_lists);
        listView.setAdapter(listViewAdapter);
    }

    @OnClick(R.id.close)
    public void onViewClicked() {
        finish();
    }

    public static void startActivity(Activity activity, String code) {
        Intent intent = new Intent(activity, ChooseLotteryActivity.class);
        intent.putExtra(EXTRA_CODE, code);
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        activity.startActivityForResult(intent, ChooseLotteryActivity.REPONSE_CODE);
    }
}
